package com.example.leetcode;

public class Leetcode2 {

    public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
           ListNode list=new ListNode(0);
           ListNode ret=list;
           ListNode tmp=new ListNode(0);;
           while (l1!=null || l2!=null){
               if(l1!=null){
                   tmp.val+= l1.val;
                   l1=l1.next;
               }
               if(l2!=null){
                   tmp.val+= l2.val;
                   l2=l2.next;
               }
               list.next=new ListNode(tmp.val%10);
               tmp.val= tmp.val/10;
               list=list.next;
           }
           if(tmp.val!=0){
               list.next=tmp;
           }
           return ret.next;
    }

    public static void main(String[] args) {


        ListNode head1=new ListNode(1);
        ListNode head2=new ListNode(2);
        head1.next=head2;

        ListNode head3=new ListNode(3);
        head2.next=head3;

        ListNode head11=new ListNode(9);
        ListNode head21=new ListNode(2);
        head11.next=head21;

        ListNode head31=new ListNode(9);
        head21.next=head31;
        ListNode listNode = new Leetcode2().addTwoNumbers(head1, head11);
        System.out.println(listNode.val);
    }
}
